package com.yonyou.iuap.ipaas.connector.sdwcoatencent.internal.operation;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.yonyou.iuap.ipaas.connector.sdk.api.model.ResponseAttributes;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.api.model.common.BatchSaveResponse;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.api.model.common.PageableData;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.api.model.common.UrlConstant;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.api.model.request.base.*;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.api.model.request.common.CommonQueryRequest;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.api.model.response.base.*;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.internal.LxwcoatencentConnection;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.internal.exception.ExecuteErrorsProvider;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.internal.utils.JSON;
import com.yonyou.yonlinker.ysb.runtime.extension.api.annotation.error.Throws;
import com.yonyou.yonlinker.ysb.runtime.extension.api.annotation.param.Connection;
import com.yonyou.yonlinker.ysb.runtime.extension.api.annotation.param.Content;
import com.yonyou.yonlinker.ysb.runtime.extension.api.annotation.param.MediaType;
import com.yonyou.yonlinker.ysb.runtime.extension.api.runtime.operation.Result;

import java.util.List;

@Throws(ExecuteErrorsProvider.class)
public class BaseOperations {

    /**
     * 物料档案列表查询
     * @param connection
     * @param proQueryRequest
     * @return
     */
    @MediaType(
            strict = false,
            value = "application/json"
    )
    public Result<String, ResponseAttributes> queryProductList(@Connection LxwcoatencentConnection connection, @Content CommonQueryRequest proQueryRequest) {
        PageableData<ProQueryResponse> result = connection.doQuery(UrlConstant.PRODUCT_QUERY_URL, JSONUtil.parseObj(proQueryRequest));
        return connection.returnCommonResult("queryProductList", result);
    }

    /**
     * 物料自定义档案列表查询
     * @param connection
     * @param proDefQueryRequest
     * @return
     */
    @MediaType(
            strict = false,
            value = "application/json"
    )
    public Result<String, ResponseAttributes> queryProductDefList(@Connection LxwcoatencentConnection connection, @Content CommonQueryRequest proDefQueryRequest) {
        PageableData<ProDefQueryResponse> result = connection.doQuery(UrlConstant.PRODUCT_DEF_QUERY_URL,JSONUtil.parseObj(proDefQueryRequest));
        return connection.returnCommonResult("queryProductDefList", result);
    }

    /**
     * 物料SKU档案列表查询
     * @param connection
     * @param skuQueryRequest
     * @return
     */
    @MediaType(
            strict = false,
            value = "application/json"
    )
    public Result<String, ResponseAttributes> queryProductSkuList(@Connection LxwcoatencentConnection connection, @Content SkuQueryQueryRequest skuQueryRequest) {
        PageableData<SkuQueryResponse> result = connection.doQuery(UrlConstant.PRODUCT_SKU_QUERY_URL, JSONUtil.parseObj(skuQueryRequest));
        return connection.returnCommonResult("queryProductSkuList", result);
    }

    /**
     * 店铺列表查询
     * @param connection
     * @return
     */
    @MediaType(
            strict = false,
            value = "application/json"
    )
    public Result<String, ResponseAttributes> queryShopList(@Connection LxwcoatencentConnection connection) {
        PageableData<ShopQueryResponse> result = connection.doQuery(UrlConstant.SHOP_QUERY_URL, null);
        return connection.returnCommonResult("queryShopList", result);
    }

    /**
     * 供应商档案列表查询
     * @param connection
     * @param supplierQueryRequest
     * @return
     */
    @MediaType(
            strict = false,
            value = "application/json"
    )
    public Result<String, ResponseAttributes> querySupplierList(@Connection LxwcoatencentConnection connection, @Content CommonQueryRequest supplierQueryRequest) {
        PageableData<SupplierQueryResponse> result = connection.doQuery(UrlConstant.SUPPLIER_QUERY_URL, JSONUtil.parseObj(supplierQueryRequest));
        return connection.returnCommonResult("querySupplierList", result);
    }

    /**
     * 仓库档案列表查询
     * @param connection
     * @param warehouseQueryRequest
     * @return
     */
    @MediaType(
            strict = false,
            value = "application/json"
    )
    public Result<String, ResponseAttributes> queryWarehouseList(@Connection LxwcoatencentConnection connection, @Content WarehouseQueryRequest warehouseQueryRequest) {
       // PageableData<WarehouseQueryResponse> result = connection.doQuery(UrlConstant.WAREHOUSE_QUERY_URL, JSON.writeValueAsString(warehouseQueryRequest));
        PageableData<WarehouseQueryResponse> result = connection.doQuery(UrlConstant.WAREHOUSE_QUERY_URL, JSONUtil.parseObj(warehouseQueryRequest));
        return connection.returnCommonResult("queryWarehouseList", result);
    }

    /**
     * 物料档案同步redis缓存
     * @param connection
     * @param materialSyncRequests
     * @return
     */
    @MediaType(
            strict = false,
            value = "application/json"
    )
    public Result<String, ResponseAttributes> syncMaterial(@Connection LxwcoatencentConnection connection, @Content List<MaterialSaveRequest> materialSyncRequests) {
        String resultStr = connection.doSecurePost(UrlConstant.REFRESH_PRODUCTS_URL, JSON.writeValueAsString(materialSyncRequests));
        BatchSaveResponse response = JSONUtil.toBean(resultStr, BatchSaveResponse.class, true);
        Result.Builder<String, ResponseAttributes> builder = Result.builder();
        ResponseAttributes ra = new ResponseAttributes();
        ra.setSuccess(true);
        ra.setCode("0");
        ra.setMessage("成功");
        builder.output(JSONUtil.toJsonStr(response));
        return builder.attributes(ra).build();
    }
}
